from rest_framework import permissions

class UserIsSelfOrReadOnly(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        if request.method in permissions.SAFE_METHODS:
            return True
        return request.user.id == obj.user.id


class UserIsSelf(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        return request.user.id == obj.id 

class IsAuthorOrReadOnly(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        if request.method in permissions.SAFE_METHODS:
            return True
        return request.user.id == obj.author.user.id or request.user.is_staff


class CanDeleteCommentOrReadOnly(permissions.BasePermission):
    def has_object_permission(self, request, view, obj):
        if request.method in permissions.SAFE_METHODS:
            return True
        user_id = request.user.id
        # return user_id == obj.author.user.id or user_id == obj.article.author.user.id
        return user_id == obj.author.user.id or request.user.is_staff
